﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class AGVCorrelationRepository : BaseRepository<TAGVPointMessage>, IAGVCorrelationRepository
    {
        public AGVCorrelationRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }

        /// <summary>
        /// 添加AGV任务类型
        /// </summary>
        /// <param name="tAGVBinDingPoint"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>

        public bool AddAGVBinDingPoint(out string msg,TAGVBinDingPoint tAGVBinDingPoint)
        {
            msg = "";
            var dictltems = Context.TDictItems.Where(x => x.Id == tAGVBinDingPoint.TaskType).ToList();
            if(dictltems.Count <= 0 )
            {
                msg = "暂无此业务类型";
            }
            
            var agvBinDingList = Context.TAGVBinDingPoint.Where(x => x.TaskType == tAGVBinDingPoint.TaskType && x.TaskClassification == tAGVBinDingPoint.TaskClassification).ToList();
            if(agvBinDingList.Count > 0)
            {
                msg = "已存在此业务类型相关信息";
                return false;
            }
            tAGVBinDingPoint.TaskTypeName = dictltems.First().ItemName;
            tAGVBinDingPoint.TaskType = dictltems.First().Id;
            Context.TAGVBinDingPoint.Add(tAGVBinDingPoint);
            if (Context.SaveChanges() <= 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 增加点位信息
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool AddAGVPointMessage(TAGVPointMessage message)
        {

            var addOk = Context.TAGVPointMessage.Add(message);
            if (Context.SaveChanges() <= 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 删除AGV任务类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool DelAGVBinDingPoint(int id)
        {
            var model = Context.TAGVBinDingPoint.Find(id);
            Context.TAGVBinDingPoint.Remove(model);
            if (Context.SaveChanges() <= 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 删除点位信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelAGVPointMessage(int id)
        {
            var model = Context.TAGVPointMessage.Find(id);
            Context.TAGVPointMessage.Remove(model);
            if (Context.SaveChanges() <= 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 查询全部AGV任务类型
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<TAGVBinDingPoint> GetAllDingPoints(int pageIndex,int pageSize,out int totalCount)
        {
            var list = Context.TAGVBinDingPoint.ToList();
            totalCount = list.Count;
            return list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
        }

        /// <summary>
        /// 查询全部点位信息
        /// </summary>
        /// <returns></returns>
        public List<TAGVPointMessage> GetAllMessage(int pageIndex, int pageSize, out int totalCount)
        {
            var list = Context.TAGVPointMessage.ToList();
            totalCount = list.Count;
            return list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
        }

        /// <summary>
        /// 根据id查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public TAGVBinDingPoint GetDingPointById(int id)
        {
            var model = Context.TAGVBinDingPoint.Find(id);
            return model;
        }

        /// <summary>
        /// 根据Id查询点位信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public TAGVPointMessage GetMessageById(int id)
        {
            var model = Context.TAGVPointMessage.Find(id);
            return model;
        }

        /// <summary>
        /// 修改AGV任务类型
        /// </summary>
        /// <param name="tAGVBinDingPoint"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool UpdAGVBinDingPoint(TAGVBinDingPoint tAGVBinDingPoint)
        {
            var upd = Context.TAGVBinDingPoint.Update(tAGVBinDingPoint);
            if (Context.SaveChanges() <= 0)
            {
                return false;
            }
            return true;

        }

        /// <summary>
        /// 修改点位信息
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool UpdAGVPointMessage(TAGVPointMessage message)
        {
            var upd = Context.TAGVPointMessage.Update(message);
            if (Context.SaveChanges() <= 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 获取AGV分组
        /// </summary>
        /// <returns></returns>
        public List<string> GetAGVGroup()
        {
            List<string> list = new List<string>();
            var agvGroupList = Context.TDictItems.Where(x => x.TypeId == 1019);
            foreach(var item in agvGroupList)
            {
                string agvGroup = item.ItemName;
                list.Add(agvGroup);
            }
            return list;
        }

    }
}
